# Copyright (c) 2008-2009 Mikhail Chernov 
#
# This file is part of dcExplorer.
#
# dcExplorer is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation, either version 2 of the License, or
# (at your option) any later version.
#
# dcExplorer is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with dcExplorer.  If not, see <http://www.gnu.org/licenses/>.

# Scripting class with extensions.
package dcExplorer::core::Group;
use strict;
use utf8;
use core::Object;
use base 'dcExplorer::core::Object';
use WeakRef;

# Construction methods

sub new 
{
	my $proto = shift;
	my $class = ref($proto) || $proto;
	my $self = $class->SUPER::new;
	weaken($self->{Parent} = shift);
	$self->{'x'} = shift;
	$self->{'y'} = shift;
	@{$self->{Units}} = ();
	bless ($self, $class);
	return $self;
}

sub removeUnit
{
	my $self = shift;
	my $unit = shift;
	@{$self->{Units}} = grep {$_->getProp('id') != $unit->getProp('id')} @{$self->{Units}};
}

sub addUnit
{
	my $self = shift;
	my $unit = shift;
	push @{$self->{Units}}, $unit;
}

# Internal engine methods

sub getEmpire
{
	my $self = shift;
	return $self->{Parent}->getEmpire();
}

# Scripting interface

sub createFleetNear
{
	my $self = shift;
	my $fleetName = shift;
	# TODO: Implement.
	die ref($self)."::createFleetNear is not implemented yet.";
}

sub getUnitByID
{
	my $self = shift;
	my $unitId = shift;
	return (grep $_->getProp('id') == $unitId, @{$self->{Units}})[0];
}

sub getUnitsByClass
{
	my $self = shift;
	my $classId = shift;
	return grep $_->getProp('building_id') == $classId, @{$self->{Units}};
}

sub getAllUnits
{
	my $self = shift;
	return @{$self->{Units}};
}

1;
